package org.linlinjava.litemall.brand.web;

import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.brand.dto.BrandCategoryMsg;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallBrandCategoryService;
import org.linlinjava.litemall.db.service.LitemallBrandService;
import org.linlinjava.litemall.db.service.LitemallCategoryService;
import org.linlinjava.litemall.db.service.LitemallServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand/brandCategory")
@Validated
public class BrandBrandCategoryController {
    @Autowired
    private LitemallCategoryService categoryService;
    @Autowired
    private LitemallBrandCategoryService brandCategoryService;
    @Autowired
    private LitemallServiceService serviceService;

    /**
     * @Description  查询公司拥有检测种类的情况，或者查询可以检测某个种类的公司情况
     * @Author Riko
     * @Date 2019/9/21
     **/
    @PostMapping("getAllBrandAndCategory")
    public Object getAllBrandAndCategory(@RequestBody String body, @LoginUser Integer userId) {
        Integer brandId = serviceService.getBelong(userId);
        Integer categoryId = JacksonUtil.parseInteger(body, "categoryId");
        Integer page = JacksonUtil.parseInteger(body, "page");
        Integer pageSize = JacksonUtil.parseInteger(body, "pageSize");
        if (page == null || pageSize == null) {
            return ResponseUtil.badArgument();
        }
        else {
            List<BrandCategoryMsg> list = new ArrayList<>();
            List<LitemallBrandCategory> allBrandAndCategory = brandCategoryService.getAllBrandAndCategory(brandId, categoryId, page, pageSize);
            for (LitemallBrandCategory litemallBrandCategory : allBrandAndCategory) {
                BrandCategoryMsg brandCategoryMsg = new BrandCategoryMsg();
                LitemallCategory category = categoryService.findById(litemallBrandCategory.getCategoryId());
                brandCategoryMsg.setCategoryName(category.getName());
                brandCategoryMsg.setPicUrl(litemallBrandCategory.getPicUrl());
                brandCategoryMsg.setRemark(litemallBrandCategory.getRemark());
                brandCategoryMsg.setId(litemallBrandCategory.getId());
                brandCategoryMsg.setCategoryId(litemallBrandCategory.getCategoryId());
                brandCategoryMsg.setBrandId(litemallBrandCategory.getBrandId());
                list.add(brandCategoryMsg);
               // brandCategoryMsg.se
            }
            return ResponseUtil.ok(list);
        }
    }
    
    /**
     * @Description  新增公司可以进行检测的L2类别    2020.5.15 改为新增必须是L3 级别
     * @Author Riko
     * @Date 2019/9/21
     **/
    @PostMapping("addCategoryForBrand")
    public Object addCategoryForBrand(@RequestBody String body, @LoginUser Integer userId) {
        Integer brandId = serviceService.getBelong(userId);
        List<Integer> categoryList = JacksonUtil.parseIntegerList(body, "categoryList");
        Integer page = JacksonUtil.parseInteger(body, "page");
        if(null == page || page <=0 ){page=1;}
        Integer limit = JacksonUtil.parseInteger(body, "limit");
        if(null == limit || limit <=0 ){limit=10;}
        //1商户ID 空   2分类List  空或数量小于1    报错
        if (brandId == null || categoryList == null || categoryList.size() < 1) {
            return ResponseUtil.badArgument();
        }
        else {
            try {
                for (Integer cid: categoryList) {
                    List<LitemallBrandCategory> list = brandCategoryService.getAllBrandAndCategory(brandId, cid, page, limit);
                    if (list != null && list.size() > 0) {
                        throw new Exception("此处禁止重复添加数据");
                    }
                    LitemallBrandCategory brandCategory = new LitemallBrandCategory();
                    brandCategory.setBrandId(brandId);
                    brandCategory.setCategoryId(cid);
                    brandCategory.setRemark("请添加描述");
                    brandCategory.setPicUrl("http://yanxuan.nosdn.127.net/7093cfecb9dde1dd3eaf459623df4071.png");
                    brandCategoryService.addBrandCategory(brandCategory);
                }
            } catch (Exception e) {
                return ResponseUtil.fail(500, e.toString());
            }
            return ResponseUtil.ok();
        }
    }
    
    /**
     * @Description  删除公司可以进行检测的项目
     * @Author Riko
     * @Date 2019/9/21
     **/
    @PostMapping("deleteCategoryForBrand")
    public Object deleteCategoryForBrand(@RequestBody String body, @LoginUser Integer userId) {
        Integer brandId = serviceService.getBelong(userId);
        List<Integer> categoryList = JacksonUtil.parseIntegerList(body, "categoryList");
        if (brandId == null || categoryList == null || categoryList.size() < 1) {
            return ResponseUtil.badArgument();
        }
        else {
            try {
                brandCategoryService.deleteCategoryForBrand(brandId, categoryList);
            } catch (Exception e) {
                return ResponseUtil.fail(500, e.toString());
            }
            return ResponseUtil.ok();
        }
    }
    
    /**
     * @Description  筛选获得公司列表
     * @Author Riko
     * @Date 2019/9/21
     **/
/*    @PostMapping("queryByCondition")
    public Object queryByCondition(@RequestBody String body, HttpServletRequest request) {
        Integer region = JacksonUtil.parseInteger(body, "region");
        Integer pCategory = JacksonUtil.parseInteger(body, "pCategory");
        List<Integer> categoryList = JacksonUtil.parseIntegerList(body, "category");
        List<Integer> scale = JacksonUtil.parseIntegerList(body, "scale");
        List<Integer> qualification = JacksonUtil.parseIntegerList(body, "qualification");
        Integer page = JacksonUtil.parseInteger(body, "page");
        Integer pageSize = JacksonUtil.parseInteger(body, "pageSize");
        String sort = JacksonUtil.parseString(body, "sort");
        String order = JacksonUtil.parseString(body, "order");
        if (page == null || pageSize == null) {
            return ResponseUtil.badArgument();
        }
        Map<Object, Object> result = new HashMap<>();
        result.put("totalNum", brandService.getTotalNumByNum(region, pCategory, categoryList, scale, qualification));
        result.put("list", brandService.queryByCondition(region, pCategory, categoryList, qualification, scale, sort, order, page, pageSize));
        return ResponseUtil.ok(result);
    }*/

//    @GetMapping("index")
//    public Object index(@LoginUser Integer userId) {
//        Integer brandId = serviceService.getBelong(userId);
//        //商户:分类中间表中拿到  商户的分类
//        List<LitemallBrandCategory> allBrandAndCategory = brandCategoryService.getAllBrandAndCategory(brandId, null, 1, 100);
//
//        // 所有一级分类目录
//        List<LitemallCategory> l1CatList = categoryService.queryL1();
////        所有子分类列表
//        List<LitemallCategory> sub;
//        for (LitemallCategory category : l1CatList) {
//            List<LitemallCategory> dec = new ArrayList<>();
//            sub = categoryService.queryByPid(category.getId());
//            for (LitemallCategory litemallCategory : sub) {
//                boolean flag = false;
//                for (LitemallBrandCategory litemallBrandCategory : allBrandAndCategory) {
//
//                    if (litemallBrandCategory.getCategoryId().equals(litemallCategory.getId())){
//                       flag=true;
//                       break;
//                    }
//                }
//                if (!flag) {
//                    dec.add(litemallCategory);
//                }
//            }
//            category.setChildCategory(dec);
//        }
//        return ResponseUtil.ok(l1CatList);
//    }

    @PostMapping("update")
    public Object update(@RequestBody LitemallBrandCategory litemallBrandCategory) {
        brandCategoryService.update(litemallBrandCategory);
        return ResponseUtil.ok();
    }


    /**
     *  商户 检测项目  可选择项目  categoryL1 >>> categoryL2 >>>  categoryL3  2020.5.1未来改浩楠
     * @param userId
     * @return
     */
    @GetMapping("index")
    public Object index(@LoginUser Integer userId) {
        Integer brandId = serviceService.getBelong(userId);
        //商户:分类中间表中拿到  商户的允许   运营的类别
        List<LitemallBrandCategory> allBrandAndCategory = brandCategoryService.getAllBrandAndCategory(brandId, null, 1, 100);

        // 所有一级分类目录
        List<LitemallCategory> l1CatList = categoryService.queryL1();
//        所有子分类列表
        List<LitemallCategory> sub;
        for (LitemallCategory categoryL1 : l1CatList) {
            //dec放入不能检测的类别
            List<LitemallCategory> decL1 = new ArrayList<>();

            List<LitemallCategory> categoryL2List = categoryService.queryByPid(categoryL1.getId());
            //L1 的子类是 L2List
            for (LitemallCategory categoryL2 : categoryL2List) {
                List<LitemallCategory> decL2 = new ArrayList<>();
                List<LitemallCategory> categoryL3List = categoryService.queryByPid(categoryL2.getId());
               // categoryL2.setChildCategory(categoryL3List);
                for (LitemallCategory categoryL3 : categoryL3List) {
                    boolean flag = false;
                    //遍历商户可以运营的类别都是 L3 级别
                    for (LitemallBrandCategory brandCategory : allBrandAndCategory) {
                        //如果  L3级别  和商户运行级别相同 跳出
                        if (brandCategory.getCategoryId().equals(categoryL3.getId())){
                            flag =  true;
                            break;
                        }
                    }
                    //如果  false    商户和L3级别   跳出
                    if ( !flag ) {
                        decL2.add(categoryL3);
                    }
                    categoryL2.setChildCategory(decL2);
                }
            }
            categoryL1.setChildCategory(categoryL2List);
        }
        return ResponseUtil.ok(l1CatList);
    }





//    @GetMapping("index")
//    public Object index(@LoginUser Integer userId) {
//        Integer brandId = serviceService.getBelong(userId);
//        List<LitemallBrandCategory> allBrandAndCategory = brandCategoryService.getAllBrandAndCategory(brandId, null, 1, 100);
//
//        // 所有一级分类目录
//        List<LitemallCategory> l1CatList = categoryService.queryL1();
//        //遍历得到L1级别的分类
//        for (LitemallCategory categoryL1 : l1CatList) {
//            //得到所有的 L2分类
//            List<LitemallCategory> categoryL2List = categoryService.queryByPid(categoryL1.getId());
//            categoryL1.setChildCategory(categoryL2List);
//            for (LitemallCategory categoryL2 : categoryL2List) {
//                List<LitemallCategory> categoryListL3 = categoryService.queryByPid(categoryL2.getId());
//                categoryL2.setChildCategoryL2(categoryL2List);
//            }
//        }
//        return ResponseUtil.ok(l1CatList);
//    }

}

